#include "BrokerContact.h"

#include <bondlib/BondInfo.h>
#include <core/ImageMgr.h>
#include <core/StyleMgr.h>
#include <widgets/SBaseDialog.h>
#include <widgets/SImageButton.h>
#include <widgets/STabCtrl.h>

#include <QGridLayout>
#include <QLabel>
#include <QStylePainter>
#include <QVBoxLayout>

#include "../BondUtility.h"
#include "uam/UserAccountManager.h"

BrokerContact::BrokerContact(QWidget* p)
    : SPopupWindow(p), m_IsBondNCD(false), m_id(0) {
  setTitle(tr("联系方式"));

  QVBoxLayout* mainLayout = contentLayout();
  mainLayout->setSpacing(0);
  mainLayout->setContentsMargins(1, 1, 1, 1);

  QHBoxLayout* tipLayout = new QHBoxLayout;
  tipLayout->setSpacing(0);
  tipLayout->setContentsMargins(0, 0, 0, 0);
  tipLayout->setAlignment(Qt::AlignLeft);

  QLabel* tipFirst = new QLabel(tr("继续议价请与"));
  tipFirst->setStyleSheet(
      QString("font:13px Microsoft YaHei;font-weight:bold;color:#%1")
          .arg(StyleMgr::instance().color("Highlight").rgb(), 0, 16));
  tipLayout->addWidget(tipFirst);
  // 按照brokerId修改文字
  m_brokerTips = new QLabel(tr(""));
  m_brokerTips->setFixedSize(80, 24);
  m_brokerTips->setAlignment(Qt::AlignCenter);

  tipLayout->addWidget(m_brokerTips);
  QLabel* tipThird = new QLabel(tr("相关经纪人联系"));
  tipThird->setStyleSheet(
      QString("font:13px Microsoft YaHei;font-weight:bold;color:#%1")
          .arg(StyleMgr::instance().color("Highlight").rgb(), 0, 16));
  tipLayout->addWidget(tipThird);

  mainLayout->addLayout(tipLayout);

  m_contactLayout = new QHBoxLayout;

  mainLayout->addLayout(m_contactLayout);

  QPushButton* closeBtn = addBtn(QMessageBox::Ok, tr("关闭"));
  connect(closeBtn, &QPushButton::clicked, this, &BrokerContact::closeBtn);
  connect(p, &SBaseDialog::close, this, &BrokerContact::closeBtn);
}

void BrokerContact::UpdataContactList(int brokerId, int bondIndex) {
  const CBondInfo& bi = CBondContainer::instance().ElementAtR(bondIndex);
  const CBrokerList& strBroker =
      SSUserAccountManager::instance().GetReadonlyBroker();

  CBrokerList::BrokerMap::const_iterator it =
      strBroker.GetBrokerMap().find(brokerId);
  if (it == strBroker.GetBrokerMap().end()) return;
  const CBrokerUnit& stUnit = it->second;

  m_brokerTips->setText(QString::fromLocal8Bit(stUnit.m_brokerName.c_str()));
  m_brokerTips->setStyleSheet(
      QString("background-color:#%1;font:13px Microsoft "
              "YaHei;font-weight:bold;color:#%2")
          .arg(BondUtility::brokerColor(brokerId).rgb(), 0, 16)
          .arg(StyleMgr::instance().color("ContactBlack").rgb(), 0, 16));
  m_contactList = stUnit.m_ContactList;

  m_IsBondNCD = bi.IsBondNCD();

  m_id = brokerId;

  QGridLayout* grid = new QGridLayout();
  grid->setSpacing(20);
  grid->setVerticalSpacing(5);
  m_contactLayout->addLayout(grid);

  std::list<xQBTradeCmpContact_c>::iterator itor = m_contactList.begin();

  bool bHaveNCD = false;
  bool bHaveNCDContact = false;

  int row = 0;
  int col = 0;

  if (m_IsBondNCD) {
    itor = m_contactList.begin();
    for (; itor != m_contactList.end(); itor++) {
      if (strcmp(itor->m_Dept, "NCD") == 0) {
        bHaveNCDContact = true;
        break;
      }
    }

    if (bHaveNCDContact) {
      QLabel* ncdLabel = new QLabel("NCD");
      ncdLabel->setStyleSheet(
          QString("font:13px Microsoft YaHei;font-weight:bold;color:#%1")
              .arg(StyleMgr::instance().color("ContactTip").rgb(), 0, 16));
      // ncdLabel->setStyleSheet(QString("font-cloor:"));
      grid->addWidget(ncdLabel, 0, col);

      row = 0;
      itor = m_contactList.begin();
      for (; itor != m_contactList.end(); itor++) {
        if (strcmp(itor->m_Dept, "NCD") == 0) {
          drawContent(itor, grid, ++row, col);
        }
      }
      col += 2;
    }
  }

  if (!bHaveNCDContact) {
    QLabel* rateLabel = new QLabel(tr("利率债"));
    rateLabel->setStyleSheet(
        QString("font:13px Microsoft YaHei;font-weight:bold;color:#%1")
            .arg(StyleMgr::instance().color("ContactTip").rgb(), 0, 16));
    grid->addWidget(rateLabel, 0, col);

    row = 0;
    itor = m_contactList.begin();
    for (; itor != m_contactList.end(); itor++) {
      if (QString::fromLocal8Bit(itor->m_Dept).compare(QString("利率债")) ==
          0) {
        drawContent(itor, grid, ++row, col);
      }
    }
    col += 2;

    QLabel* creditLabel = new QLabel(tr("信用债"));
    creditLabel->setStyleSheet(
        QString("font:13px Microsoft YaHei;font-weight:bold;color:#%1")
            .arg(StyleMgr::instance().color("ContactTip").rgb(), 0, 16));
    grid->addWidget(creditLabel, 0, col);

    row = 0;
    itor = m_contactList.begin();
    for (; itor != m_contactList.end(); itor++) {
      if (QString::fromLocal8Bit(itor->m_Dept).compare(QString("信用债")) ==
          0) {
        drawContent(itor, grid, ++row, col);
      }

      if (QString::fromLocal8Bit(itor->m_Dept).compare(QString("资金")) == 0)
        bHaveNCD = true;
    }
    col += 2;

    bool First = true;
    row = 0;
    itor = m_contactList.begin();
    for (; itor != m_contactList.end(); itor++) {
      if (QString::fromLocal8Bit(itor->m_Dept).compare(QString("高收益")) ==
          0) {
        if (First) {
          QLabel* highYieldLabel = new QLabel(tr("高收益债"));
          highYieldLabel->setStyleSheet(
              QString("QLabel{font:13px Microsoft "
                      "YaHei;font-weight:bold;color:#%1}")
                  .arg(StyleMgr::instance().color("ContactTip").rgb(), 0, 16));

          grid->addWidget(highYieldLabel, 0, col);
        }
        First = false;

        drawContent(itor, grid, ++row, col);
      }
    }
    col += 2;
  }

  if (m_id == 1 && m_IsBondNCD && bHaveNCD) {
    QLabel* capitalLabel = new QLabel(tr("资金"));
    capitalLabel->setStyleSheet(
        QString("font:13px Microsoft YaHei;font-weight:bold;color:#%1")
            .arg(StyleMgr::instance().color("ContactTip").rgb(), 0, 16));
    grid->addWidget(capitalLabel, 0, col);

    row = 0;
    itor = m_contactList.begin();
    for (; itor != m_contactList.end(); itor++) {
      if (QString::fromLocal8Bit(itor->m_Dept).compare(QString("资金")) == 0) {
        drawContent(itor, grid, ++row, col);
      }
    }
  }
}

void BrokerContact::closeBtn() { close(); }

void BrokerContact::drawContent(
    std::list<xQBTradeCmpContact_c>::iterator content, QGridLayout* grid,
    int row, int col) {
  QLabel* name = new QLabel(QString::fromLocal8Bit(content->m_Contact));
  name->setStyleSheet(
      QString("font:13px Microsoft YaHei;font-weight:bold;color:#%1")
          .arg(StyleMgr::instance().color("TextNormal").rgb(), 0, 16));
  grid->addWidget(name, row, col);

  QLabel* tel = new QLabel(QString::fromLocal8Bit(content->m_TEL));
  tel->setStyleSheet(
      QString("font:13px Microsoft YaHei;font-weight:bold;color:#%1")
          .arg(StyleMgr::instance().color("Highlight").rgb(), 0, 16));
  grid->addWidget(tel, row, col + 1);
}

//
